package com.kern.rpcclient;


import com.kern.rpc.bean.Request;

import java.lang.reflect.Proxy;

/**
 * Created by caoshi at 20:29 2021-07-21
 */
public class RpcClientProxy {
    private RpcClient sock = new RpcClient();

    @SuppressWarnings("unchecked")
    public <T> T getProxy(Class<T> clazz) {
        return (T) Proxy.newProxyInstance(clazz.getClassLoader(),
                new Class<?>[]{clazz}, (proxy, method, args) -> {
                    Request request = new Request();
                    request.setClassName(method.getDeclaringClass().getName());
                    request.setMethodName(method.getName());
                    request.setParamTypes(method.getParameterTypes());
                    request.setParams(args);
                    return sock.invoke(request, "127.0.0.1", 12000);
                });
    }

}
